package com.me.study.producerAndConsumer;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 
 *
 * @author ME
 * @date   2019/3/6 20:04
 */
public class Main {

    public static ReentrantLock lock = new ReentrantLock();
    public static Condition empty = lock.newCondition();
    public static Condition full = lock.newCondition();

    public static void main(String[] args) {
        List<PCData> queue = new ArrayList<PCData>();
        int length = 10;

        ExecutorService service = Executors.newCachedThreadPool();

        for (int i = 0; i < 5; i++) {
            service.execute(new Producer(queue, length));
        }
        for (int i = 0; i < 10; i++) {
            service.execute(new Consumer(queue));
        }
        // Producer p1 = new Producer(queue,length);
        // Producer p2 = new Producer(queue,length);
        // Producer p3 = new Producer(queue,length);
        // Consumer c1 = new Consumer(queue);
        // Consumer c2 = new Consumer(queue);
        // Consumer c3 = new Consumer(queue);
        // service.execute(p1);
        // service.execute(p2);
        // service.execute(p3);
        // service.execute(c1);
        // service.execute(c2);
        // service.execute(c3);
    }

    public static class Producer implements Runnable{
        private List<PCData> queue;
        private int len;

        public Producer(List<PCData> queue, int len){
            this.queue = queue;
            this.len = len;
        }

        @Override
        public void run() {
            try{
                while(true){
                    if(Thread.currentThread().isInterrupted()) {
                        break;
                    }
                    Random r = new Random();
                    PCData data = new PCData();
                    data.setData(r.nextInt(500));
                    Main.lock.lock();
                    if(queue.size() >= len)
                    {
                        Main.empty.signalAll();
                        Main.full.await();
                    }
                    Thread.sleep(1000);
                    queue.add(data);
                    Main.lock.unlock();
                    System.out.println("生产者ID:"+Thread.currentThread().getName()+" 生产了:"+data.getData());
                }
            }catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public static class Consumer implements Runnable{
        private List<PCData> queue;

        public Consumer(List<PCData> queue){
            this.queue = queue;
        }

        @Override
        public void run() {
            try {
                while (true) {
                    if (Thread.currentThread().isInterrupted())
                        break;
                    PCData data = null;
                    Main.lock.lock();
                    if (queue.size() == 0){
                        Main.full.signalAll();
                        Main.empty.await();
                    }
                    Thread.sleep(1000);
                    data = queue.remove(0);
                    Main.lock.unlock();
                    System.out.println("消费者ID:"+Thread.currentThread().getName()+" 消费了:"+data.getData()+" result:"+data.getData());
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }

    public static class PCData {
        private int data;

        public int getData() {
            return data;
        }

        public void setData(int data) {
            this.data = data;
        }
    }
}
